<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Giaoban extends CI_Controller 
{
	
	public	function __construct()
	{
		parent::__construct();
		$this->load->library('pagination');
		if($this->session->userdata('user_id')){
			//$this->load->helper("url");
			$this->load->model("giaobans");
			$this->load->model("users");
			//$this->load->model("lich");
			date_default_timezone_set('UTC');
		}
		else{
			redirect('login');
		}
	}
	
	public function index($type=1)
	{
		//$this->viewAllGiaoban();
		$this->viewPageGiaoban($type,0);
	}
	public function viewAllGiaoban()
	{		
		$data['allnew'] = $this->giaobans->getAllGiaoban();
		$menu['menu'] = "thongbao";
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view('giaoban/viewAllGiaoban',$data);
		$this->load->view('footer_block');
	}
	public function viewPageGiaoban($type=1,$id=null)
	{
		if($this->input->post("date"))
			$date = $this->input->post("date");
		else 
			$date = null;
		$total_row = $this->giaobans->getSoluongGiaoban($type);
		$data['allnew'] = $this->giaobans->getPage($date,$type,$id);
		$this->paging($date,$type,$total_row);
		$menu['menu'] = $type;
		$data['type'] = $type;
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view('giaoban/viewAllGiaoban',$data);
		$this->load->view('footer_block');
	}
	public function viewGiaoban($type=1,$id_giaoban)
	{
		$data['new'] = $this->giaobans->getGiaoban($id_giaoban);
		$menu['menu'] = $data['new']->type_giaoban;
		$data["type"] = $type;
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view('giaoban/viewGiaoban',$data);
		$this->load->view('footer_block');
	}
	
	public function viewAddGiaoban($type=1)
	{
		$quyen01 = $this->users->getQuyen($this->session->userdata("user_id"));
		if(count($quyen01) > 0){
			foreach($quyen01 as $q){
				$quyen[] = $q->id_quyen;
			}
		}
		else
			$quyen = array();
		//if(in_array(3, $quyen)) {
			$menu['menu'] = $type;			
			$this->load->view('header_block_v1',$menu);
			$this->load->view('content_sidebar_block',$menu);
			$this->load->view('giaoban/viewAddGiaoban',$menu);
			$this->load->view('footer_block');
		/* }
		else 
			redirect('giaoban'); */
	}
	public function addGiaoban($type=1)
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('tieude_tin', 'Tiêu đề giao ban', 'xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('noidung_tin', 'Nội dung giao ban', 'xss_clean|required|min_length[5]');
		if ($this->form_validation->run() == FALSE){
			$this->viewAddGiaoban($type);
		}
		else {
			$data["tieude_giaoban"] = $this->input->post("tieude_tin");
			$data["noidung_giaoban"] = $this->input->post("noidung_tin");
			$data["type_giaoban"] = $type;
			$data["id_nguoidung"] = $this->session->userdata("user_id");
			$data["ngay_giaoban"] = date("Y-m-d H:i:s");
			switch($type)
			{
				case 1:
					$url = base_url()."giaoban";
					break;
				case 2:
					$url = base_url()."nhanxet";
					break;		
				case 3:
					$url = base_url()."diembao";
					break;
				case 4:
					$url = base_url()."detai";
					break;
			}
			if($this->giaobans->insertGiaoban($data)){
				echo "<script>alert('Thêm thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không insert được!');	window.location='".$url."'; </script>";
		}
	}
	public function viewEditGiaoban($type=1,$id_giaoban)
	{
		$quyen01 = $this->users->getQuyen($this->session->userdata("user_id"));
		if(count($quyen01) > 0){
			foreach($quyen01 as $q){
				$quyen[] = $q->id_quyen;
			}
		}
		else
			$quyen = array();
		$data['giaoban'] = $this->giaobans->getGiaoban($id_giaoban);
		$menu['menu'] = $type;
		$data["type"] = $type;
		$this->load->view('header_block_v1',$menu);
		$this->load->view('content_sidebar_block',$menu);
		$this->load->view('giaoban/viewEditGiaoban',$data);
		$this->load->view('footer_block');
	}
	public function editGiaoban($type=1,$id_giaoban)
	{
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		//dieu kien form
		$this->form_validation->set_rules('tieude_tin', 'Tiêu đề thông báo', 'xss_clean|required|min_length[5]');
		$this->form_validation->set_rules('noidung_tin', 'Nội dung thông báo', 'xss_clean|required|min_length[5]');
		if ($this->form_validation->run() == FALSE){
			$this->viewEditGiaoban($type,$id_giaoban);
		}
		else {
			$data["tieude_giaoban"] = $this->input->post("tieude_tin");
			$data["noidung_giaoban"] = $this->input->post("noidung_tin");
			$data["ngay_giaoban"] = date("Y-m-d H:i:s");
			switch($type)
				{
					case 1:
						$url = base_url()."giaoban";
						break;
					case 2:
						$url = base_url()."nhanxet";
						break;		
					case 3:
						$url = base_url()."diembao";
						break;
					case 4:
						$url = base_url()."detai";
						break;
				}
			if($this->giaobans->updateGiaoban($data,$id_giaoban)){
				echo "<script>alert('Bạn đã sửa thành công');	window.location='".$url."'; </script>";
			}
			else
				echo "<script>alert('Lỗi: Không update được!');	window.location='".$url."'; </script>";
		}
	}
	public function delGiaoban($id_giaoban)
	{
		switch($type)
		{
			case 1:
				$url = base_url()."giaoban";
				break;
			case 2:
				$url = base_url()."nhanxet";
				break;		
			case 3:
				$url = base_url()."diembao";
				break;
			case 4:
				$url = base_url()."detai";
				break;
		}
		if($this->giaobans->delGiaoban($id_giaoban)){
			echo "<script>alert('Bạn đã xóa thành công');	window.location='".$url."'; </script>";
		}
		else
			echo "<script>alert('Lỗi: Không del được!');	window.location='".$url."'; </script>";
	}
	
	function paging($date,$type,$total_row){
		$this->load->library('pagination');
		$config['base_url']=site_url("giaoban/viewPageGiaoban/".$type."/");
		$config['total_rows']=$total_row;
		$config['per_page']=15;
		$config['full_tag_open'] = "<div class='news_pagination'>";
        $config['full_tag_close'] = '</div>';
        $config['first_link'] = 'Trang đầu';
        $config['first_tag_open'] = "<div class='news_page next'>";
        $config['first_tag_close'] = '</div>';
        $config['last_link'] = 'Trang cuối';
        $config['last_tag_open'] = "<div class='news_page next'>";
        $config['last_tag_close'] = '</div>';
        $config['next_link'] = 'Trang tiếp';
        $config['next_tag_open'] = "<div class='news_page next'>";
        $config['next_tag_close'] = '</div>';
        $config['prev_link'] = 'Trang trước';
        $config['prev_tag_open'] = "<div class='news_page next'>";
        $config['prev_tag_close'] = '</div>';
        $config['cur_tag_open'] = "<div class='news_page number curr_page'>";
        $config['cur_tag_close'] = '</div>';
        $config['num_tag_open'] = "<div class='news_page number'>";
        $config['num_tag_close'] = '</div>';
		$this->pagination->initialize($config);
	}
	
}